package com.gaoyi.crm.db;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.gaoyi.crm.db.converter.DateConverter;
import com.gaoyi.crm.db.dao.CategoryDao;
import com.gaoyi.crm.db.dao.CustomerDao;
import com.gaoyi.crm.db.dao.ItemNoStoreDao;
import com.gaoyi.crm.db.dao.PmnDao;
import com.gaoyi.crm.db.dao.PmnItemNoDao;
import com.gaoyi.crm.db.dao.PmnAttachDao;
import com.gaoyi.crm.db.dao.TableVersionDao;
import com.gaoyi.crm.db.entity.Category;
import com.gaoyi.crm.db.entity.Customer;
import com.gaoyi.crm.db.entity.ItemNoStore;
import com.gaoyi.crm.db.entity.Pmn;
import com.gaoyi.crm.db.entity.PmnAttach;
import com.gaoyi.crm.db.entity.PmnItemNo;
import com.gaoyi.crm.db.entity.TableVersion;
import com.gaoyi.crm.util.AppUtil;
import com.gaoyi.crm.util.ThreadManager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Database(entities = {Customer.class, PmnAttach.class, PmnItemNo.class, Pmn.class
        , Category.class, ItemNoStore.class, TableVersion.class}
        , version = 1, exportSchema = false)
@TypeConverters({DateConverter.class})
public abstract class CrmDatabase extends RoomDatabase {
    private static final String DB_NAME = "SynCrm.db";
    public static final String TABLE_CUSTOMER = "mst_Customer";
    public static final String TABLE_CATEGORY = "mst_Category";
    public static final String TABLE_ITEM_NO_STORE = "mst_ItemNoStore";
    public static final String TABLE_PMN = "tim_Pmn";
    public static final String TABLE_PMN_ITEM_NO = "tim_PmnItemNo";
    public static final String TABLE_PMN_ATTACH = "tim_PmnAttach";
    public static final String TABLE_VERSION = "sys_TableVersion";
    private static final String[] SYNC_TABLE = new String[]{ "Customers",  "Categorys", "ItemNoStores"};
    private static volatile CrmDatabase INSTANCE;

    public static String getSyncTableName(int index) {
        return SYNC_TABLE[index];
    }

    public abstract CustomerDao getCustomerDao();

    public abstract PmnAttachDao getPmnTaskAttachDao();

    public abstract PmnItemNoDao getPmnItemNoDao();

    public abstract PmnDao getPmnDao();

    public abstract CategoryDao getCategoryDao();

    public abstract ItemNoStoreDao getItemNoStoreDao();

    public abstract TableVersionDao getTableVersionDao();

    public static synchronized CrmDatabase getDatabase() {
        if (INSTANCE == null) {
            synchronized (CrmDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(AppUtil.getContext()
                                    , CrmDatabase.class, DB_NAME)
                            .addCallback(new Callback() {
                                @Override
                                public void onCreate(@NonNull SupportSQLiteDatabase db) {
                                    super.onCreate(db);
                                    initTableVersion();
                                }
                            })
                            .build();
                }
            }
        }
        return INSTANCE;
    }

    /**
     * 创建数据库时初始化数据表的版本号
     *
     */
    private static void initTableVersion() {
        ThreadManager.getInstance().execute(() -> {
            TableVersionDao versionDao = getDatabase().getTableVersionDao();
            Date date = new Date();
            List<TableVersion> list = new ArrayList<>();
            for (String s : SYNC_TABLE) {
                TableVersion tableVersion = new TableVersion();
                tableVersion.setObjName(s);
                tableVersion.setSynVer(0);
                tableVersion.setCreatedAt(date);
                list.add(tableVersion);
            }
            versionDao.inserts(list);
        });
    }
}
